package com.ilop.permission;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

import com.permissionx.guolindev.PermissionX;

/**
 * @author skygge
 * @date 6/15/22.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class PermissionUtil {

    //TODO: 检查定位权限是否都已经获取（包括定位权限及定位服务是否打开）
    public static boolean onCheckLocationPermission(Context context) {
        boolean permission = onGetLocationPermission(context);
        boolean enabled = onGetLocationEnabled(context);
        return permission && enabled;
    }

    //TODO: 获取定位权限状态
    public static boolean onGetLocationPermission(Context context) {
        return (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
    }

    //TODO: 获取定位服务开关状态
    public static boolean onGetLocationEnabled(Context context) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        if (locationManager != null) {
            boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            return gps || network;
        } else return false;
    }

    //TODO: 设置定位权限
    public static void onSetLocationPermissions(FragmentActivity activity, PermissionsCallBack callBack) {
        onCheckLocationPermissions(activity, Manifest.permission.ACCESS_FINE_LOCATION, callBack);
    }

    //TODO: 获取相机权限状态
    public static boolean onGetCameraPermission(Context context) {
        return (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED);
    }

    //TODO: 设置相机权限
    public static void onSetCameraPermissions(FragmentActivity activity, PermissionsCallBack callBack) {
        onCheckLocationPermissions(activity, Manifest.permission.CAMERA, callBack);
    }

    //TODO: 申请权限
    private static void onCheckLocationPermissions(FragmentActivity activity, String permissions, PermissionsCallBack callBack){
        PermissionX.init(activity).permissions(permissions).onForwardToSettings((scope, deniedList) ->
                        scope.showForwardToSettingsDialog(deniedList, activity.getString(R.string.allow_permissions),
                                activity.getString(R.string.to_set), activity.getString(R.string.cancel)))
                .request((allGranted, grantedList, deniedList) -> {
                    if (allGranted) callBack.onRequestAllGranted();
                });
    }

    /**
     * 跳转到应用消息推送设置界面
     */
    public static void goToAppNotificationSettings(Context context) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            //直接跳转到应用通知设置的代码：
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
            }else {
                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                intent.putExtra("app_package", context.getPackageName());
                intent.putExtra("app_uid", context.getApplicationInfo().uid);
            }
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("pushPermission有问题");
            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", context.getPackageName(), null));
            context.startActivity(intent);
        }
    }

    /**
     * 判断该app是否打开了通知
     * */
    public static boolean isNotificationEnabled(Context context) {
        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
        return notificationManagerCompat.areNotificationsEnabled();
    }

    public interface PermissionsCallBack {

        void onRequestAllGranted();
    }
}
